import store from '@/vue/store'
import { Routes } from '@vue/router/routes'

export const multLang = key => store.getters['lang/get'](key)
export const tplLang = (key, langs) => store.getters['lang/tpl'](key, langs)

export const getToken = () => store.getters['login/get']('token')
export const clearToken = () => store.dispatch('login/setToken', '')

export const login = () => store.dispatch('login/setOnline', true)
export const logout = () => store.dispatch('login/setOnline', false)

export const setActiveMenu = name => store.dispatch('menu/setActiveMenu', name)
export const clearActiveMenu = () =>
  store.dispatch('menu/setActiveMenu', Routes.Admin)

//返回所有菜单map
export const getMenusMap = () => store.getters['menu/get']('menusMap')
//根据route,查找菜单对象,返回{route,langkey}
export const getMenuMap = key => store.getters['menu/getMap'](key)
//根据route,是否有菜单,返回true,false
export const includeMenusMap = route =>
  getMenusMap().some(item => item.route === route)

//返回当前打开的所有菜单map
export const getActiveMenus = () => store.getters['menu/get']('activeMenus')
//添加菜单对象{route,langkey},到打开菜单map
export const addActiveMenu = data => store.dispatch('menu/addActiveMenu', data)
//清空 所有打开菜单
export const clearActiveMenus = () => store.dispatch('menu/clearActiveMenus')
//根据route,判断是否打开菜单,返回true,false
export const includeActiveMenus = (route, menus) => {
  const some = item => item.route === route
  return menus ? menus.some(some) : getActiveMenus().some(some)
}
export const delActiveMenu = route => {
  const menus = getActiveMenus()
  let name = ''
  includeActiveMenus(route, menus) &&
    menus.forEach(
      (item, i) =>
        item.route === route &&
        (store.dispatch('menu/delActiveMenu', i), (name = menus[--i].route))
    )
  return name
}
